package com.zlz.mapreduce.wordcount;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author: zlz
 * @date: 2021/12/31 22:29
 * @description:
 * @since: JDK1.8
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final Text outKey = new Text();
    // map阶段不进行聚合，val直接写为1
    private final IntWritable outValue = new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取一行的数据
        String line = value.toString();

        // 分割数据
        String[] words = StringUtils.split(line, StringUtils.SPACE);

        // 循环写出
        for (String word : words) {
            outKey.set(word);
            context.write(outKey, outValue);
        }
    }
}
